package com.projecy.product.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.projecy.product.mapper.ProductMaterialDetailMapper;
import com.projecy.product.mapper.ProductMaterialMapper;
import com.projecy.product.model.ProductMaterial;
import com.projecy.product.vo.ProductMaterialDetailVo;
import com.projecy.product.vo.ProductMaterialVo;
import com.projecy.product.service.ProductMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("productMaterialService")
public class ProductMaterialServiceImpl extends ServiceImpl<ProductMaterialMapper, ProductMaterial> implements ProductMaterialService {
    @Autowired
    ProductMaterialMapper productMaterialMapper;
    @Autowired
    ProductMaterialDetailMapper productMaterialDetailMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean insertProductMaterial(ProductMaterialVo productMaterialVo) {

        Integer price=0;
        for (ProductMaterialDetailVo productMaterialDetailVo : productMaterialVo.getProductMaterialDetailVos()) {
            price+=productMaterialDetailVo.getNumber()*productMaterialDetailVo.getPrice();
        }

        productMaterialVo.setTotal(price);

        productMaterialMapper.insertProductMaterial(productMaterialVo);

        Boolean flag=false;

        ProductMaterialDetailVo[] arr = productMaterialVo.getProductMaterialDetailVos();
        List<ProductMaterialDetailVo> list = Arrays.asList(arr);
        for (ProductMaterialDetailVo productMaterialDetailVo : list) {
            productMaterialDetailVo.setSubtotal(productMaterialDetailVo.getNumber()*productMaterialDetailVo.getPrice());
            productMaterialDetailVo.setProductId(productMaterialVo.getDid());
            flag=productMaterialDetailMapper.insertProductMaterialDetai(productMaterialDetailVo);
        }


        return flag;
    }
}
